package com.example.pets.view.fragment.adapter;



import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.pets.R;
import com.example.pets.model.entities.Pet;


import java.util.List;

public class PetAdapter extends RecyclerView.Adapter<PetAdapter.PetViewHolder> {

    private Context context;
    private List<Pet> mpetList;

    public PetAdapter(Context context, List<Pet> petList) {
        this.context = context;
        this.mpetList = petList;
    }

    public void refreshdata( List<Pet> petList){
        mpetList.clear();
        mpetList.addAll(petList);
        notifyDataSetChanged();
    }



    @NonNull
    @Override
    public PetViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_pet, parent, false);
        return new PetViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PetViewHolder holder, int position) {
        Pet pet = mpetList.get(position);

        holder.tvPetName.setText(pet.getPname());
        holder.tvPetBreed.setText("品种：" + pet.getBreed());

        if (pet.getGender() != null) {
            holder.tvPetGender.setText(pet.getGender() ? "性别：公" : "性别：母");
        } else {
            holder.tvPetGender.setText("性别：未知");
        }

        holder.tvPetBirth.setText("年龄：" + pet.getBirth());

        // 使用 Glide 加载图片
        if (pet.getImageUrl() != null && !pet.getImageUrl().isEmpty()) {
            Glide.with(context)
                    .load(pet.getImageUrl())
                    .into(holder.ivPetImage);
        }
    }

    @Override
    public int getItemCount() {
        return mpetList.size();
    }

    static class PetViewHolder extends RecyclerView.ViewHolder {
        ImageView ivPetImage;
        TextView tvPetName, tvPetBreed, tvPetGender, tvPetBirth;

        public PetViewHolder(@NonNull View itemView) {
            super(itemView);
            ivPetImage = itemView.findViewById(R.id.iv_pet_image);
            tvPetName = itemView.findViewById(R.id.tv_pet_name);
            tvPetBreed = itemView.findViewById(R.id.tv_pet_breed);
            tvPetGender = itemView.findViewById(R.id.tv_pet_gender);
            tvPetBirth = itemView.findViewById(R.id.tv_pet_birth);
        }
    }
}
